<?php
namespace StarCloud\Library\Bosskg;

use StarCloud\Library\Exception;

class BosskgPay
{
    const channels = [
        1 => [
            'providerId' => '555005',
            'taskId' => '1610278112283336274',
            'payType' => 0,
        ],
        2 => [
            'providerId' => '35524',
            'taskId' => '1610278112283326252',
            'payType' => 2,
        ]
    ];
    private $providerId = '555005';
    private $taskId = '1610278112283336274';
    private $payType = 2;

    public function __construct($type = 1)
   {
       require_once PLATFORM_LIB_PATH ."/Bosskg/sby/HttpUtlis.php";
       $this->providerId = self::channels[$type]['providerId'];
       $this->taskId = self::channels[$type]['taskId'];
       $this->payType = self::channels[$type]['payType'];
   }

   public function decrypt($data)
   {
       $httpUtlis = new \HttpUtlis();
       return $httpUtlis->decrypt($data);
   }

   public function sign($openId, $name, $idCard, $phone)
   {
       $httpUtlis = new \HttpUtlis();
       $a =  [
           'name'=> $name,
           'idCard'=>$idCard,
           'CardNo'=> $openId,
           'Mobile'=>$phone,
           'ProviderId'=>$this->providerId,
           'PaymentType'=> $this->payType,
       ];

       $res = $httpUtlis->buildRequest('6010',$a);
       $data = $httpUtlis->sendToYouFu($res);

       if (!empty($data)) {
           $json = json_decode($data, true);
           if ($json['resCode'] == '6016' || $json['resCode'] == '0000') {
               return true;
           }
           return false;
       }

       return false;
   }

   public function checkSign($name, $idCard, $phone)
   {
       $httpUtlis = new \HttpUtlis();
       $a =  [
           'name'=> $name,
           'idCard'=>$idCard,
           'Mobile'=>$phone,
           'ProviderId'=>$this->providerId,
       ];

       $res = $httpUtlis->buildRequest('6011',$a);
       $data = $httpUtlis->sendToYouFu($res);

       if (!empty($data)) {
           \Phalcon\Di::getDefault()->get("logger")->info("checkSign request: " . json_encode($a) . ' reponse ' . $data);
           $json = json_decode($data, true);
           if ($json['resCode'] == '0000') {
               if ($json['resData']['state'] == 1) {
                   return true;
               }

               if ($json['resData']['state'] == 3) {
                    throw new Exception(9999, '用户正在签约中');
               }

               return false;
           }

           return false;
       }

       return false;
   }

   public function pay($openId, $name, $idCard, $mobile, $orderNo, $money, $payType = 2)
   {
       $httpUtlis = new \HttpUtlis();
       $data = [
           'merBatchId' => date("YmdHis") . mt_rand(10000000, 99999999),
           'taskId' => $this->taskId,
           'providerId' => $this->providerId,
           'payItems' => [
               [
                   'merOrderId'=> $orderNo,
                   'idCard' => $idCard,
                   'payeeAcc' => $openId,
                   'payeeName' => $name,
                   'amt'=> $money * 100,
                   'mobile' => $mobile,
                   'ProviderId' => $this->providerId,
                   'paymentType' => $this->payType,
                   'memo' => '抖啦咪数据助手',
                   'notifyUrl'=>'https://api.doumvip.com/api/pay/tianjin_notify'
               ]
           ]
       ];

       $res = $httpUtlis->buildRequest('6001', $data);
       $data = $httpUtlis->sendToYouFu($res);

       if (!empty($data)) {
           $json = json_decode($data, true);
           if ($json['resCode'] == '0000') {
               return $json;
           }
           throw new Exception(9999, $json['resMsg'] ?? '系统错误');
       }

       throw new Exception(9999, '系统错误001');
   }
}